package com.beordie.dao.impl;

import com.beordie.dao.IDriverDao;
import com.beordie.model.Driver;
import com.beordie.utils.DBUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Classname DriverDaoImpl
 * @Description TODO
 * @Date 2021/7/13 17:28
 * @Created 30500
 */
public class DriverDaoImpl extends DBUtils implements IDriverDao {
    @Override
    public List<Driver> getAllDrivers() {
        List<Driver> drivers = new ArrayList<>();
        try {
            resultSet = super.query(SQL_SELECT_ALL, null);
            while (resultSet.next()) {
                Driver driver = new Driver();
                driver.setId(resultSet.getInt("id"));
                driver.setName(resultSet.getString("name"));
                driver.setAddress(resultSet.getString("address"));
                driver.setPostCard(resultSet.getString("postcard"));
                driver.setPhone(resultSet.getString("phone"));
                driver.setCreateTime(resultSet.getDate("createtime"));
                driver.setNumber(resultSet.getString("number"));
                drivers.add(driver);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeAll();
        }
        return drivers;
    }

    @Override
    public List<Driver> getAllByLimit(boolean isLimit, int start, int size) {
        if (!isLimit)
            return getAllDrivers();
        List<Integer> params = new ArrayList<>();
        params.add(start);
        params.add(size);

        List<Driver> drivers = new ArrayList<>();
        try {
            resultSet = super.query(SQL_SELECT_ALL_BY_LIMIT, params);
            while (resultSet.next()) {
                Driver driver = new Driver();
                driver.setId(resultSet.getInt("id"));
                driver.setName(resultSet.getString("name"));
                driver.setAddress(resultSet.getString("address"));
                driver.setPostCard(resultSet.getString("postcard"));
                driver.setPhone(resultSet.getString("phone"));
                driver.setCreateTime(resultSet.getDate("createtime"));
                driver.setNumber(resultSet.getString("number"));
                drivers.add(driver);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeAll();
        }
        return drivers;
    }

    @Override
    public int insert(Driver driver) {
        List<Object> params = new ArrayList<>();
        params.add(driver.getName());
        params.add(driver.getAddress());
        params.add(driver.getPostCard());
        params.add(driver.getPhone());
        params.add(driver.getCreateTime());
        params.add(driver.getNumber());

        int result = 0;
        try {
            result = super.update(SQL_INSERT_ONE, params);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public int deleteById(int id) {
        List<Integer> params = new ArrayList<>();
        params.add(id);

        int result = 0;
        try {
            result = super.update(SQL_DELETE_BY_ID, params);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public int getCount() {
        int result = 0;
        try {
            resultSet = super.query(SQL_SELECT_COUNT, null);
            if (resultSet.next())
                result = resultSet.getInt(1);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public int updateById(Driver driver, int id) {
        List<Object> params = new ArrayList<>();
        params.add(driver.getName());
        params.add(driver.getAddress());
        params.add(driver.getPostCard());
        params.add(driver.getPhone());
        params.add(driver.getCreateTime());
        params.add(driver.getNumber());
        params.add(id);

        int result = 0;
        try {
            result = super.update(SQL_UPDATE_BY_ID, params);
        } catch (Exception e) {
            e.printStackTrace();
            result = -1;
        } finally {
            super.closeAll();
        }
        return result;
    }

    @Override
    public Driver findByNumName(String condition) {
        List<String> params = new ArrayList<>();
        params.add(condition);
        Driver driver = null;
        try {
            resultSet = super.query(SQL_SELECT_BY_NUMBER, params);
            if (resultSet.next()) {
                driver = new Driver();
                driver.setId(resultSet.getInt("id"));
                driver.setName(resultSet.getString("name"));
                driver.setAddress(resultSet.getString("address"));
                driver.setPostCard(resultSet.getString("postcard"));
                driver.setPhone(resultSet.getString("phone"));
                driver.setCreateTime(resultSet.getDate("createtime"));
                driver.setNumber(resultSet.getString("number"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            super.closeAll();
        }
        return driver;
    }
}
